unit fmCustomSelect;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, dxSkinsCore, dxSkinOffice2007Black, Menus, ActnList,
  StdCtrls, cxButtons, cxGroupBox, cxStyles, dxSkinscxPCPainter,
  cxCustomData, cxFilter, cxData, cxDataStorage, DB, cxDBData, cxGridLevel,
  cxClasses, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBAccess, Uni, MemDS;

type
  TCustomSelectForm = class(TForm)
    pnlAll: TcxGroupBox;
    grpButtons: TcxGroupBox;
    btnApply: TcxButton;
    btnClose: TcxButton;
    CustomSelectActionList: TActionList;
    actSelect: TAction;
    actCancel: TAction;
    tblSelect: TUniTable;
    dsSelect: TUniDataSource;
    grdSelect: TcxGrid;
    grdSelectView: TcxGridDBTableView;
    grdSelectLevel1: TcxGridLevel;
    procedure FormCreate(Sender: TObject);
    procedure actSelectUpdate(Sender: TObject);
    procedure actCancelExecute(Sender: TObject);
    procedure grdSelectViewDblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    class function ExecuteId(AOwner: TComponent; var AName: string; ACurrentId: integer = -1): integer;
  end;

implementation

{$R *.dfm}

{ TCustomSelectForm }

class function TCustomSelectForm.ExecuteId(AOwner: TComponent; var AName: string; ACurrentId: integer): integer;
begin
  Result := -1;
  with Create(AOwner) do
  try
    if ACurrentId > 0 then
      tblSelect.Locate('id', ACurrentId, []);
      
    if ShowModal = mrOk then
    begin
      Result := tblSelect.Fields[0].AsInteger;
      if AName <> '' then
        AName := tblSelect.FieldByName(AName).AsString;
    end;
  finally
    Free;
  end;
end;

procedure TCustomSelectForm.FormCreate(Sender: TObject);
begin
  tblSelect.Open;
end;

procedure TCustomSelectForm.actSelectUpdate(Sender: TObject);
begin
  if actSelect.Enabled <> ((grdSelectView.ViewData.RecordCount <> 0) and (grdSelectView.Focused))then
    actSelect.Enabled := ((grdSelectView.ViewData.RecordCount <> 0) and (grdSelectView.Focused));
end;

procedure TCustomSelectForm.actCancelExecute(Sender: TObject);
begin
  Close;
end;

procedure TCustomSelectForm.grdSelectViewDblClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

end.
